/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.coraci.anag.gui.components.basic.gui;

import it.coraci.anag.db.mongo.entities.Attribute;
import it.coraci.anag.gui.components.basic.gui.logic.CRUDParametrizablePanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Luca
 */
public class SimpleBooleanParamPanel extends javax.swing.JPanel implements CRUDParametrizablePanel{
    
    public Attribute attribute;

    /**
     * Creates new form ParamPanel
     */
    public SimpleBooleanParamPanel() {
        initComponents();
    }

    public SimpleBooleanParamPanel(Attribute attribute) {
        initComponents();
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jCheckBox1.setSelected(Boolean.parseBoolean(attribute.getValue()));
        }
    }
    
    

    public void setAttribute(Attribute attribute) {
        this.attribute = attribute;
        this.jLabel1.setText(attribute.getKey());
        if (attribute.getValue() != null) {
            this.jCheckBox1.setSelected(Boolean.parseBoolean(attribute.getValue()));
        }
    }

    public Attribute getAttribute() {
        attribute.setKey(this.jLabel1.getText());
        attribute.setValue(""+jCheckBox1.isSelected());
        return attribute;
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ParamKey:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void setEditable(boolean editable) {
        this.jCheckBox1.setEnabled(editable);
    }

    @Override
    public boolean isEditable() {
        return this.jCheckBox1.isEnabled();
    }
    
    @Override
    public Object getValue() {
        return this.getAttribute().getValue();
    }
    
    
    @Override
    public void setValue(Object value) {
        if (value instanceof Boolean) {
            this.jCheckBox1.setSelected((Boolean)value);
            this.attribute.setValue(((Boolean)value).toString());
        }
    }

    @Override
    public void clear() {
        this.jCheckBox1.setSelected(false);
    }
}
